<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取公共配置
     *
     */
    public function config()
    {
        $top_config = db('header_config')->find();//顶部配置
        $foot_config = db('foot_config')->find();//底部配置
        $config = db('config')->where('group','basic')->column('value','name');//基本配置
        $extension_config = db('config')->where('group','personal_center')->column('value','name');//推广配置
        $links = db('links')->where('status','normal')->select();//友情链接
        $publish_configuration = db('config')->where('group','publish_configuration')->column('value','name');//发布配置

        $this->success('',[
            'top_config' => $top_config,
            'foot_config' => $foot_config,
            'title' => $config['name'],
            'logo' => $config['logo'],
            'promotion_rules' => $extension_config['promotion_rules'],
            'member_service' => $extension_config['member_service'],
            'links' => $links,
            'url' => request()->domain(),
            'publish_configuration' => $publish_configuration,
        ]);
    }

    /**
     * 获取分类
     */
    public function category(){
        $list = db('category')->where('status','normal')->select();

        $data = getTree($list);

        $this->success('',$data);
    }

    /**
     * 获取上边轮播图
     */
    public function getHomebanner(){
        $list = db('home_banner')->where('status','normal')->select();

        $this->success('',$list);
    }

    /**
     * 获取中间6个图标
     */
    public function getHomeIcon(){
        $list = db('home_icon')->where('status','normal')->select();

        $this->success('',$list);
    }


    /**
     * 获取下边3个卖点
     */
    public function getHomeIntroduce(){
        $list = db('home_introduce')->where('status','normal')->select();

        $this->success('',$list);
    }

    /**
     * 获取新闻资讯分类
     */
    public function newsCategory(){
        $list = db('new_category')->where('status','normal')->select();

        $this->success('',$list);
    }

    /**
     * 获取新闻资讯列表
     */
    public function newList(){
        $category_id = input('param.category_id');

        $page = input('param.page',1);
        $limit = input('param.limit',10);

        $list = db('news')->where('category_id',$category_id)->where('status','normal')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        $this->success('',$data);
    }

    public function newDetails(){
        $id = input('param.id');
        $list = db('news')->where('status','normal')->order('createtime desc')->select();
        $info = [];
        $pointer = 0;
        foreach ($list as $key => $value){
            if($value['id'] == $id){
                $info = $value;
                $pointer = $key;
            }
        }
        if($info){
            $info['lower_info'] = ($pointer - 1 >= 0 && isset($list[$pointer - 1])) ? $list[$pointer - 1] : null;
            $info['upper_info'] = isset($list[$pointer + 1]) ? $list[$pointer + 1] : null;
        }
        $this->success('',$info);
    }

    /**
     * 获取新闻banner
     */
    public function newBanner(){
        $list = db('new_banner')->where('status','normal')->select();

        $this->success('',$list);
    }

    /***
     * 首页置顶需求列表(左边)
     */
    public function homeDemandTopList(){
        $page = input('param.page',1);
        $limit = input('param.limit',10);

        $where['to_examine'] = 1;
        $list = db('demand')->where('status','normal')->where($where)->order('is_top desc,createtime desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['one_category_name'] = db('category')->where('id',$value['one_category_id'])->value('name');
            $data['data'][$key]['two_category_name'] = db('category')->where('id',$value['two_category_id'])->value('name');
            $data['data'][$key]['tree_category_name'] = db('category')->where('id',$value['tree_category_id'])->value('name');
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
        }
        $this->success('',$data);
    }

    /***
     * 推广需求列表(右边)
     */
    public function homeDemandExtensionList(){
        $page = input('param.page',1);
        $limit = input('param.limit',10);
        $where['to_examine'] = 1;
        $list = db('demand')->where('is_extension',1)->where($where)->where('status','normal')->order('is_extension desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        if(count($data['data']) <= 0){
            $list = db('demand')->where('is_extension',1)->where($where)->where('status','normal')->order('is_extension desc')->paginate($limit,false,['page'=>1]);
            $data = tp_builde_page($list);
        }
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['one_category_name'] = db('category')->where('id',$value['one_category_id'])->value('name');
            $data['data'][$key]['two_category_name'] = db('category')->where('id',$value['two_category_id'])->value('name');
            $data['data'][$key]['tree_category_name'] = db('category')->where('id',$value['tree_category_id'])->value('name');
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
        }
        $this->success('',$data);
    }

    /***
     * 推广求购列表(右边)
     */
    public function wantToBuyExtensionList(){
        $user = $this->auth->getUser();

        $page = input('param.page',1);
        $limit = input('param.limit',10);
        $where['to_examine'] = 1;
        $list = db('want_to_buy')->where('is_extension',1)->where($where)->where('status','normal')->order('is_extension desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        if(count($data['data']) <= 0){
            $list = db('want_to_buy')->where('is_extension',1)->where($where)->where('status','normal')->order('is_extension desc')->paginate($limit,false,['page'=>1]);
            $data = tp_builde_page($list);
        }
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
            if($user && $user->is_member == 1){

            }else{
                $data['data'][$key]['contacts_tel'] = substr_replace($value['contacts_tel'], "********", 3, 7);;
            }
        }
        $this->success('',$data);
    }

    /***
     * 推广服务列表(右边)
     */
    public function serviceExtensionList(){
        $user = $this->auth->getUser();

        $page = input('param.page',1);
        $limit = input('param.limit',10);
        $where['to_examine'] = 1;
        $list = db('service')->where('is_extension',1)->where($where)->where('status','normal')->order('is_extension desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        if(count($data['data']) <= 0){
            $list = db('service')->where('is_extension',1)->where($where)->where('status','normal')->order('is_extension desc')->paginate($limit,false,['page'=>1]);
            $data = tp_builde_page($list);
        }
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
            if($user && $user->is_member == 1){

            }else{
                $data['data'][$key]['contacts_tel'] = substr_replace($value['contacts_tel'], "********", 3, 7);;
            }
        }
        $this->success('',$data);
    }


    /**
     * 最新产品信息
     */
    public function newOrderCount(){
        $str=date("Y-m-d",strtotime("-1 day"))." 0:0:0";
        $yesterday_star = strtotime($str);
        $str=date("Y-m-d",strtotime("-1 day"))." 23:59:59";
        $yesterday_end = strtotime($str);

        $m = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y')));
        $t = date('t',strtotime($m)); //上个月共多少天

        $begintime = strtotime(date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))));
        $endtime = strtotime(date('Y-m-d', mktime(0,0,0,date('m')-1,$t + 1,date('Y'))));

        $yesterday_count = db('demand_order')->where('createtime','between',[$yesterday_star,$yesterday_end])->where('pay_status',1)->count();
        $lastmonth_count = db('demand_order')->where('createtime','between',[$begintime,$endtime])->where('pay_status',1)->count();
        $count =db('demand_order')->where('pay_status',1)->count();

        $data = [
            'yesterday_count' => $yesterday_count,
            'lastmonth_count' => $lastmonth_count,
            'count' => $count,
        ];

        $this->success('',$data);
    }


























}
